package com.hui.controller;

import com.hui.pojo.bo.UserBo;
import com.hui.pojo.Users;
import com.hui.service.UserService;
import com.hui.utils.CookieUtils;
import com.hui.utils.JsonUtils;
import com.hui.utils.MD5Utils;
import com.hui.utils.MyJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Api(value = "注册登录",tags = {"用户注册登录相关接口"})
@RestController
@RequestMapping("passport")
public class PassportController {
    @Autowired
    private UserService userService;


    @ApiOperation(value = "用户名用户注册",notes = "用户注册",httpMethod = "POST")
    @PostMapping("/regist")
    public MyJSONResult regist(@RequestBody UserBo userBo,
                               HttpServletRequest request,
                               HttpServletResponse response){
        String password = userBo.getPassword();
        String username = userBo.getUsername();
        String confirmPassword = userBo.getConfirmPassword();

        //用户名和密码不为空
        if(StringUtils.isBlank(username)||
            StringUtils.isBlank(password)||
            StringUtils.isBlank(confirmPassword)){
            return MyJSONResult.errorMsg("用户名密码不为空");
        }
        //查询用户名是否存在
        boolean isExist = userService.queryUsernameIsExist(username);
        if(isExist){
            return MyJSONResult.errorMsg("用户名不能为空");
            // return HttpStatus.INTERNAL_SERVER_ERROR;
        }
        //校验密码合理
        if(password.length()<6){
            return MyJSONResult.errorMsg("密码长度不能小于6");
        }
        //判断两次密码是否一致
        if(!password.equals(confirmPassword)){
            return MyJSONResult.errorMsg("两次密码不一致");
        }

        //实现注册
        Users userResult = userService.createUser(userBo);
        userResult = setNullProperty(userResult);
        CookieUtils.setCookie(request,response,"user",
                JsonUtils.objectToJson(userResult),true);
        return MyJSONResult.ok();
    }

    @ApiOperation(value = "用户名是否存在",notes = "用户名是否存在",httpMethod = "GET")
    @GetMapping("/usernameIsExist")
    public MyJSONResult UserNameIsExist(@RequestParam String username){
        //判断传入用户名为真
        if(StringUtils.isBlank(username)){
            return MyJSONResult.errorMsg("用户名不能为空");
            //return HttpStatus.INTERNAL_SERVER_ERROR;
        }
        boolean isExist = userService.queryUsernameIsExist(username);
        if(isExist){
            return MyJSONResult.errorMsg("当前用户名已存在");
           // return HttpStatus.INTERNAL_SERVER_ERROR;
        }
        //请求成功 用户名不重复
        return MyJSONResult.ok();
    }

    @ApiOperation(value = "用户登录",notes = "验证登录",httpMethod = "POST")
    @PostMapping("/login")
    public MyJSONResult queryUserForLogin(@RequestBody UserBo userBo,
                                          HttpServletRequest request,
                                          HttpServletResponse response){
        String password = userBo.getPassword();
        String username = userBo.getUsername();
        //用户名和密码不为空
        if(StringUtils.isBlank(username)||
                StringUtils.isBlank(password)||
                password.length()<6){
           return MyJSONResult.errorMsg("账号密码格式不正确");
        }


        //实现登录
        Users userResult = null;
        try {
            userResult = userService
                    .queryUserForLogin(username, MD5Utils.getMD5Str(password));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(userResult==null){
            return MyJSONResult.errorMsg("用户名密码不正确");
        }
        userResult = setNullProperty(userResult);

        CookieUtils.setCookie(request,response,"user",
                JsonUtils.objectToJson(userResult),true);
        return MyJSONResult.ok(userResult);
    }
    private Users setNullProperty(Users userResult){
        //保护用户信息安全 不向前端发送
        userResult.setPassword(null);
        userResult.setMobile(null);
        userResult.setEmail(null);
        userResult.setUpdatedTime(null);
        userResult.setCreatedTime(null);
        userResult.setBirthday(null);
        return userResult;
    }
    @ApiOperation(value = "用户退出",notes = "清除缓存",httpMethod = "POST")
    @PostMapping("/logout")
    public MyJSONResult logout(@RequestParam String userId,
                               HttpServletRequest request,
                               HttpServletResponse response){
        //清楚用户相关信息Cookie
        CookieUtils.deleteCookie(request,response,"user");
        //TODD 退出登录清空购物车
        //todd 分布式条件下 清除缓存
        return MyJSONResult.ok();
    }
}

